﻿define(['jquery', 'kendo', 'moment', 'ajax-pre'], function ($, kendo, moment) {
    function resizeTabStripContent() {
        kendo.resize("#tabstrip");
    }
    var _postArr = [];
    var _userPostDataArr = [];
    var _userDepartDataArr = [];
    var _maleCount = 0;
    var _femaleCount = 0;
    var _ageRangeCountArr = [];
    var _passInfos=[];
    function initData() {
        var posts = JSON.parse(document.getElementById('posts').value);
        var users = JSON.parse(document.getElementById('users').value);
        var departs = JSON.parse(document.getElementById('departs').value);
        var passInfos = JSON.parse(document.getElementById('passInfos').value);
        debugger;
        var postArr = [];
        var userPostDataArr = [];
        var departArr = [];
        var userDepartDataArr = [];
        for (var i = 0; i < posts.length; i++) {
            var itemPost = posts[i];
            var postName = itemPost.PostName;
            var itemCount = 0;
            for (var j = 0; j < users.length; j++) {
                var itemUser = users[j];
                if (itemUser.PositionId == itemPost.Id) {
                    itemCount++;
                }
            }
            postArr.push(postName);
            userPostDataArr.push(itemCount);
        }
        for (var i = 0; i < departs.length; i++) {
            var itemDepart = departs[i];
            var name = itemDepart.Name;
            var itemCount = 0;
            for (var j = 0; j < users.length; j++) {
                var itemUser = users[j];
                if (itemUser.DepartmentId == itemDepart.Id) {
                    itemCount++;
                }
            }
            departArr.push(name);
            userDepartDataArr.push(itemCount);
        }
        for (var j = 0; j < users.length; j++) {
            var itemUser = users[j];            
            if (itemUser.Sex == "0") {
                _maleCount++;
            } else {
                _femaleCount++;
            }
        }
        var ageRangeArr = [10, 20, 30, 40, 50,60];
        var curDate = new Date();
        _ageRangeCountArr = [];
        for (var i = 0; i < ageRangeArr.length; i++) {
            var itemRange = ageRangeArr[i];
            var preRange = itemRange - 10;
            if (itemRange == 60) {
                itemRange = 200;//50以上
            }
            var ageCount = 0;
            for (var j = 0; j < users.length; j++) {
                var itemUser = users[j];                
                var birthYear = itemUser.BirthDay.substr(0, 4);
                birthYear = Number(birthYear);
                if (birthYear) {
                    var age = curDate.getFullYear() - birthYear;
                    if (age > preRange && age <= itemRange) {
                        ageCount++;
                    }
                }
            }
            _ageRangeCountArr.push(ageCount);
        }
        _passInfos = [];
        for (var i = 0; i < passInfos.length; i++) {
            var itemPass = passInfos[i];
            _passInfos.push({ category: itemPass.Name, value: itemPass.Count });
        }
        _postArr = postArr;
        _userPostDataArr = userPostDataArr;
        _departArr = departArr;
        _userDepartDataArr = userDepartDataArr;        
        initCharts();
    }
    initData();
    function initCharts() {
        //岗位报表
        $(".chart-post").kendoChart({
            title: {
                text: "人员岗位分布"
            },
            theme: 'metro',
            legend: {
                position: "top"
            },
            seriesDefaults: {
                type: "column"
            },
            series: [{
                name: "人员数量",
                color: "#67DF65",
                axis: "number",
                data: _userPostDataArr,
                tooltip: {
                    visible: true,
                    format: "{0:p1}",
                    template: "#= series.name #: #= kendo.toString(value,'n2') #"
                }
            }],
            valueAxis: [{
                name: 'number',
                
                line: {
                    visible: false
                }
            }],
            categoryAxis: {
                categories: _postArr,
                line: {
                    visible: true
                },
                labels: {
                    rotation: -75
                },
                axisCrossingValues: [0, 10]
            },
            tooltip: {
            }
        });
        //部门报表
        $(".chart-depart").kendoChart({
            title: {
                text: "人员部门分布"
            },
            theme: 'metro',
            legend: {
                position: "top"
            },
            seriesDefaults: {
                type: "column"
            },
            series: [{
                name: "人员数量",
                color: "#67DF65",
                axis: "number",
                data: _userDepartDataArr,
                tooltip: {
                    visible: true,
                    format: "{0:p1}",
                    template: "#= series.name #: #= kendo.toString(value,'n2') #"
                }
            }],
            valueAxis: [{
                name: 'number',

                line: {
                    visible: false
                }
            }],
            categoryAxis: {
                categories: _departArr,
                line: {
                    visible: true
                },
                labels: {
                    rotation: -75
                },
                axisCrossingValues: [0, 10]
            },
            tooltip: {
            }
        });
        //性别
        $(".chart-sex").kendoChart({
            title: {
                text: "人员性别分布"
            },
            legend: {
                position: "top"
            },
            chartArea: {
                background: ""
            },
            seriesDefaults: {
                labels: {
                    visible: true,
                    background: "transparent",
                    template: "#= category #: \n #= value#"
                }
            },
            series: [{
                type: "pie",
                startAngle: 150,
                data: [{
                    category: "男",
                    value: _maleCount,
                    color: "#428bca"
                }, {
                    category: "女",
                    value: _femaleCount,
                    color: "#f56954"
                }]
            }],
            tooltip: {
                visible: true,
                format: "{0}"
            }
        });
        // 年龄
        $(".chart-age").kendoChart({
            title: {
                text: "人员年龄分布"
            },
            legend: {
                position: "top"
            },
            chartArea: {
                background: ""
            },
            seriesDefaults: {
                labels: {
                    visible: true,
                    background: "transparent",
                    template: "#= category #: \n #= value#"
                }
            },
            series: [{
                type: "pie",
                startAngle: 150,
                data: [{
                    category: "1-10",
                    value: _ageRangeCountArr[0],
                    color: "#9de219"
                }, {
                    category: "11-20",
                    value: _ageRangeCountArr[1],
                    color: "#90cc38"
                }, {
                    category: "21-30",
                    value: _ageRangeCountArr[2],
                    color: "#068c35"
                }, {
                    category: "31-40",
                    value: _ageRangeCountArr[3],
                    color: "#006634"
                }, {
                    category: "41-50",
                    value: _ageRangeCountArr[4],
                    color: "#004d38"
                }, {
                    category: "50以上",
                    value: _ageRangeCountArr[5],
                    color: "#033939"
                }]
            }],
            tooltip: {
                visible: true,
                format: "{0}"
            }
        });
        //护照
        $(".chart-passPort").kendoChart({
            title: {
                text: "人员护照分布"
            },
            legend: {
                position: "top"
            },
            chartArea: {
                background: ""
            },
            seriesDefaults: {
                labels: {
                    visible: true,
                    background: "transparent",
                    template: "#= category #: \n #= value#"
                }
            },
            series: [{
                type: "pie",
                startAngle: 150,
                data: _passInfos
            }],
            tooltip: {
                visible: true,
                format: "{0}"
            }
        });
    }
});